package org.rsbot.script.randoms;

import java.util.logging.Logger;
import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.NPCs;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSNPC;

@ScriptManifest(authors={"PwnZ"}, name="Quiz", version=1.0D)
public class QuizSolver extends Random
{
  public int quizInterface = 191;
  public int[] Fish = { 6190, 6189 };
  public int[] Jewelry = { 6198, 6197 };
  public int[] Weapons = { 6192, 6194 };
  public int[] Farming = { 6195, 6196 };
  public int[][] items = { this.Fish, this.Jewelry, this.Weapons, this.Farming };

  public String[] names = { "Fish", "Jewelry", "Weapons", "Farming" };

  public boolean activateCondition()
  {
    RSNPC localRSNPC = this.npcs.getNearest(new String[] { "Quiz Master" });
    return localRSNPC != null;
  }

  public void atRandom() {
    atSlot(random(1, 3));
  }

  public boolean atSlot(int paramInt) {
    switch (paramInt) {
    case 1:
      return this.interfaces.getComponent(this.quizInterface, 3).doClick();
    case 2:
      return this.interfaces.getComponent(this.quizInterface, 4).doClick();
    case 3:
      return this.interfaces.getComponent(this.quizInterface, 5).doClick();
    }
    return false;
  }

  public int ID_to_Slot(int paramInt)
  {
    if (Slot_to_ID(1) == paramInt)
      return 1;
    if (Slot_to_ID(2) == paramInt)
      return 2;
    if (Slot_to_ID(3) == paramInt) {
      return 3;
    }
    return -1;
  }

  public int loop()
  {
    RSNPC localRSNPC = this.npcs.getNearest(new String[] { "Quiz Master" });
    if (localRSNPC == null) {
      return -1;
    }
    if (Slot_to_ID(1) != -1) {
      this.log.info("Question detected.");
      QuizQuestion localQuizQuestion = new QuizQuestion(Slot_to_ID(1), Slot_to_ID(2), Slot_to_ID(3));
      if (localQuizQuestion.clickAnswer()) {
        return random(2200, 3000);
      }
      this.log.info("Trying Random Answer");
      atRandom();
      return random(1200, 2200);
    }

    if (this.interfaces.clickContinue()) {
      return random(800, 1200);
    }
    return random(1200, 2000);
  }

  public int Slot_to_ID(int paramInt) {
    switch (paramInt)
    {
    case 1:
      return this.interfaces.get(this.quizInterface).getComponent(6).getComponentID();
    case 2:
      return this.interfaces.get(this.quizInterface).getComponent(7).getComponentID();
    case 3:
      return this.interfaces.get(this.quizInterface).getComponent(8).getComponentID();
    }
    return -1;
  }

  public class QuizQuestion
  {
    int ID_One;
    int ID_Two;
    int ID_Three;
    int answer;

    public QuizQuestion(int paramInt1, int paramInt2, int arg4)
    {
      this.ID_One = paramInt1;
      this.ID_Two = paramInt2;
      int i;
      this.ID_Three = i;
    }

    public boolean activateCondition()
    {
      return (QuizSolver.this.ID_to_Slot(this.ID_One) != -1) && 
        (QuizSolver.this.ID_to_Slot(this.ID_Two) != -1) && 
        (QuizSolver.this.ID_to_Slot(this.ID_Three) != -1);
    }

    public boolean arrayContains(int[] paramArrayOfInt, int paramInt)
    {
      int i = 0;
      for (int m : paramArrayOfInt) {
        if (m == paramInt) {
          i = 1;
        }
      }

      return i;
    }

    public boolean clickAnswer() {
      this.answer = -1;
      int i = 0;

      for (int j = 0; j < QuizSolver.this.items.length; j++) {
        if (arrayContains(QuizSolver.this.items[j], this.ID_One)) {
          QuizSolver.this.log.info("Slot 1: " + QuizSolver.this.names[j]);
          i++;
        }
        if (arrayContains(QuizSolver.this.items[j], this.ID_Two)) {
          QuizSolver.this.log.info("Slot 2: " + QuizSolver.this.names[j]);
          i++;
        }
        if (arrayContains(QuizSolver.this.items[j], this.ID_Three)) {
          QuizSolver.this.log.info("Slot 3: " + QuizSolver.this.names[j]);
          i++;
        }

        if (i >= 2) {
          QuizSolver.this.log.info("Type Found: " + QuizSolver.this.names[j]);
          this.answer = j;
          break;
        }
      }

      if (this.answer != -1)
      {
        if ((j = findNotInAnswerArray()) != -1) {
          return QuizSolver.this.atSlot(j);
        }
        QuizSolver.this.log.info("findNotInAnswerArray() fail.");
        return false;
      }

      QuizSolver.this.log.info("answer fail.");
      return false;
    }

    public int findNotInAnswerArray()
    {
      if (!arrayContains(QuizSolver.this.items[this.answer], this.ID_One))
        return 1;
      if (!arrayContains(QuizSolver.this.items[this.answer], this.ID_Two))
        return 2;
      if (!arrayContains(QuizSolver.this.items[this.answer], this.ID_Three)) {
        return 3;
      }
      return -1;
    }
  }
}